<template>
  <div class="user-edit-container">
    <!-- Header 区域 -->
    <van-nav-bar title="编辑资料" left-arrow @click-left="$router.back()" fixed />

    <!-- 用户资料 -->
    <van-cell-group class="action-card">
      <van-cell title="头像" is-link center>
        <template #default>
          <van-image @click="imagesClickFn" round class="avatar" :src="userObj.photo" />
          <!-- file 选择框 -->
          <input type="file" ref="iptFile" v-show="false" accept="image/*" @change="onFileChange" />
        </template>
      </van-cell>
      <van-cell @click="nameFn" title="名称" is-link :value="userObj.name" />
      <van-dialog :before-close="berforeCloseFn" v-model.trim="show" title="修改名称" show-cancel-button>
        <input v-model="userName" v-fofo />
      </van-dialog>
      <van-cell @click="bithFn" title="生日" is-link :value="userObj.birthday" />
      <!-- 弹出层 -->
      <van-popup round v-model="pShow" position="bottom" :style="{ height: '50%' }">
        <!-- 时间选择器 -->
        <van-datetime-picker @confirm="confirmFn" @cancel="cancelFn" v-model="currentDate" type="date" title="选择年月日" :min-date="minDate" :max-date="maxDate" />
      </van-popup>
    </van-cell-group>
  </div>
</template>

<script>
import { userProfileAPI, updatePhotoAPI, updateProfileAPI } from '@/api/index.js'
import { Notify } from 'vant'
import { formatDate } from '@/utils/date.js'
import { mapMutations } from 'vuex'

export default {
  name: 'UserEdit',
  data () {
    return {
      userObj: {},
      show: false,
      userName: '',
      minDate: new Date(1920, 0, 1), // 最小范围
      maxDate: new Date(), // 最大范围(默认取系统日期-今日)
      currentDate: new Date(2021, 0, 17), // 当前时间
      pShow: false
    }
  },
  async created () {
    const { data: res } = await userProfileAPI()
    this.userObj = res.data
  },
  methods: {
    ...mapMutations(['SET_USERPHOTO']),
    async onFileChange (e) {
      if (e.target.files.length === 0) return
      console.log(e.target.files[0])
      const theFd = new FormData()
      theFd.append('photo', e.target.files[0])
      const { data: res } = await updatePhotoAPI(theFd)
      this.userObj.photo = res.data.photo
      this.SET_USERPHOTO(res.data.photo)
    },
    imagesClickFn () {
      this.$refs.iptFile.click() // JS模拟标签的事件触发
    },
    nameFn () {
      this.show = true
      this.userName = this.userObj.name
    },
    // 姓名 - 确认框 - 关闭前回调函数
    async berforeCloseFn (action, done) {
      if (action === 'confirm') {
        // 点确定
        const reg = /^[A-Za-z0-9\u4e00-\u9fa5]{1,7}$/
        if (reg.test(this.userName) === true) {
          // 通过了校验
          await updateProfileAPI({
            userName: this.userName
          })
          // 更新页面显示的名字
          this.userObj.name = this.userName
          done()
        } else {
          Notify({
            type: 'warning',
            message: '用户名只能是1-7位中英文数字组合'
          })
        }
      } else {
        // 点取消
        // 关闭弹窗
        done()
      }
    },
    bithFn () {
      this.pShow = true
      this.currentDate = new Date(this.userObj.birthday)
    },
    cancelFn () {
      this.pShow = false
    },
    async confirmFn () {
      await updateProfileAPI({
        birthday: formatDate(this.currentDate)
      })
      this.userObj.birthday = formatDate(this.currentDate)
      this.pShow = false
    }
  }
}
</script>

<style lang="less" scoped>
.user-edit-container {
  padding-top: 46px;
  .avatar {
    width: 50px;
    height: 50px;
  }
}
::v-deep .van-dialog__content {
  text-align: center;
  input {
    outline: none;
    border: none;
    padding: 0;
    text-align: center;
  }
}
</style>
